package com.changpay.zywy.ui.fragment.main
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.GridLayoutManager
import android.view.View
import android.widget.ImageView
import cn.bingoogolapple.bgabanner.BGABanner
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.ShopGoodsAdapter
import com.changpay.zywy.api.WebApis
import com.changpay.zywy.base.BaseMvpFragment
import com.changpay.zywy.bean.ShopBanner
import com.changpay.zywy.bean.ShopGoodsBean
import com.changpay.zywy.mvp.contract.ShopContract
import com.changpay.zywy.mvp.presenter.ShopPresenter
import com.changpay.zywy.ui.activity.web.WebViewActivity
import com.changpay.zywy.utils.ImageLoader
import com.changpay.zywy.utils.MarginDecoration
import io.reactivex.Observable
import kotlinx.android.synthetic.main.fragment_shop_refresh_layout.*
import kotlinx.android.synthetic.main.item_shop_banner.view.*

/**
 * Created by chenxz on 2018/4/22.
 */
class ShopFragment : BaseMvpFragment<ShopContract.View, ShopContract.Presenter>(), ShopContract.View {

    override fun showBanner(data: List<ShopBanner>) {
        bannerDatas = data as ArrayList<ShopBanner>
        val bannerFeedList = ArrayList<String>()
        val bannerTitleList = ArrayList<String>()
        Observable.fromIterable(data)
            .subscribe { list ->
                bannerFeedList.add(list.adv_code)
            }
        bannerView?.banner?.run {
            setAutoPlayAble(bannerFeedList.size > 1)
            setData(bannerFeedList, bannerTitleList)
            setAdapter(bannerAdapter)
        }
    }

    override fun showShopGoods(data: List<ShopGoodsBean>) {
        data.forEach {
            it.goods_list.goods_info.data.forEach {
                mAdapter.addData(it)
            }
        }
    }

    companion object {
        fun getInstance(): ShopFragment = ShopFragment()
    }

    override fun createPresenter(): ShopContract.Presenter = ShopPresenter()

    /**
     * datas
     */
    private val datas = mutableListOf<ShopGoodsBean.GoodsListBean.GoodsInfoBean.DataBean>()

    /**
     * banner datas
     */
    private lateinit var bannerDatas: ArrayList<ShopBanner>

    /**
     * banner view
     */
    private var bannerView: View? = null

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        activity?.let {
            MarginDecoration(it)
        }
    }

    /**
     * Home Adapter
     */
    private val mAdapter: ShopGoodsAdapter by lazy {
        ShopGoodsAdapter(activity, datas)
    }

    /**
     * Banner Adapter
     */
    private val bannerAdapter: BGABanner.Adapter<ImageView, String> by lazy {
        BGABanner.Adapter<ImageView, String> { bgaBanner, imageView, feedImageUrl, position ->
            ImageLoader.load(feedImageUrl, imageView)
        }
    }

    /**
     * LinearLayoutManager
     */
    private val gridLayoutManager: GridLayoutManager by lazy {
        GridLayoutManager(activity,2)
    }

    /**
     * is Refresh
     */
    private var isRefresh = true

    override fun attachLayoutRes(): Int = R.layout.fragment_shop_refresh_layout


    override fun initView(view: View) {
        super.initView(view)
        swipeRefreshLayout.isEnabled = false

        recyclerView.run {
            layoutManager = gridLayoutManager
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            recyclerViewItemDecoration?.let { addItemDecoration(it) }
        }

        bannerView = layoutInflater.inflate(R.layout.item_shop_banner, null)
        bannerView?.banner?.run {
            setDelegate(bannerDelegate)
        }

        mAdapter.run {
            bindToRecyclerView(recyclerView)
            onItemClickListener = this@ShopFragment.onItemClickListener
//            onItemChildClickListener = this@ShopFragment.onItemChildClickListener
            setEmptyView(R.layout.fragment_empty_layout)
            addHeaderView(bannerView)
        }
    }

    override fun lazyLoad() {
        mPresenter?.getBanner()
        mPresenter?.getShop(token)
    }

    override fun showLoading() {
        // swipeRefreshLayout.isRefreshing = isRefresh
    }

    override fun hideLoading() {
    }

    override fun showError(errorMsg: String) {

    }


    /**
     * ItemClickListener
     */
    private val onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        if (datas.size != 0) {
            val data = datas[position]
            WebViewActivity.startActivity(context,WebApis.shopping_details+"?id="+data.goods_id)
//            Intent(activity, ContentActivity::class.java).run {
//                putExtra(Constant.CONTENT_URL_KEY, data.link)
//                putExtra(Constant.CONTENT_TITLE_KEY, data.title)
//                putExtra(Constant.CONTENT_ID_KEY, data.id)
//                startActivity(this)
        }
    }

    /**
     * BannerClickListener
     */
    private val bannerDelegate = BGABanner.Delegate<ImageView, String> { banner, imageView, model, position ->
        //        if (bannerDatas.size > 0) {
//            val data = bannerDatas[position]
//            Intent(activity, ContentActivity::class.java).run {
//                putExtra(Constant.CONTENT_URL_KEY, data.url)
//                putExtra(Constant.CONTENT_TITLE_KEY, data.title)
//                putExtra(Constant.CONTENT_ID_KEY, data.id)
//                startActivity(this)
//            }
//        }
    }

    /**
     * ItemChildClickListener
     */
//    private val onItemChildClickListener =
//            BaseQuickAdapter.OnItemChildClickListener { _, view, position ->
//                if (datas.size != 0) {
//                    val data = datas[position]
//                    when (view.id) {
//                        R.id.iv_like -> {
//                            if (isLogin) {
//                                if (!NetWorkUtil.isNetworkAvailable(App.context)) {
//                                    showSnackMsg(resources.getString(R.string.no_network))
//                                    return@OnItemChildClickListener
//                                }
//                                val collect = data.collect
//                                data.collect = !collect
//                                mAdapter.setData(position, data)
//                                if (collect) {
//                                    mPresenter?.cancelCollectArticle(data.id)
//                                } else {
//                                    mPresenter?.addCollectArticle(data.id)
//                                }
//                            } else {
//                                Intent(activity, LoginActivity::class.java).run {
//                                    startActivity(this)
//                                }
//                                showToast(resources.getString(R.string.login_tint))
//                            }
//                        }
//                    }
//                }
//            }
}